﻿using System;
using System.Collections.Generic;
using System.Text;
using AllegNet;
using Res = Ex08_Mp3;

namespace Ex08_Mp3
{
    class Main : Api
    {
        #region Properties
        private string _Str_ScreenResolution = "";
        private string _Str_GfxMode = "";
        private string _Str_RenderingMode = "";
        private string _Str_BmpType = "";

        public string Str_ScreenResolution
        {
            get { return _Str_ScreenResolution; }
            set { _Str_ScreenResolution = value; }
        }
        public string Str_GfxMode
        {
            get { return _Str_GfxMode; }
            set { _Str_GfxMode = value; }
        }
        public string Str_RenderingMode
        {
            get { return _Str_RenderingMode; }
            set { _Str_RenderingMode = value; }
        }
        public string Str_BmpType
        {
            get { return _Str_BmpType; }
            set { _Str_BmpType = value; }
        }
        #endregion

        internal static int Fps = 0;
        internal static int FpsToShow = 0;

        public void WinMain()
        {
            ScreenResolution ScreenRes = ScreenResolution.Size_320_240;
            GfxModes GfxMode = GfxModes.GfxAutoDetect;
            RenderingModes RenderingMode = RenderingModes.Null;
            BmpType BmpTyp = BmpType.Null;


            #region Get Params
            #region switch (Str_ScreenResolution)
            switch (Str_ScreenResolution)
            {
                case "320 x 240":
                    ScreenRes = ScreenResolution.Size_320_240;
                    break;
                case "640 x 480":
                    ScreenRes = ScreenResolution.Size_640_480;
                    break;
                case "800 x 600":
                    ScreenRes = ScreenResolution.Size_800_600;
                    break;
                case "1024 x 768":
                    ScreenRes = ScreenResolution.Size_1024_768;
                    break;
                case "1088 x 612":
                    ScreenRes = ScreenResolution.Size_1088_612;
                    break;
                case "1152 x 864":
                    ScreenRes = ScreenResolution.Size_1152_864;
                    break;
                case "1280 x 720":
                    ScreenRes = ScreenResolution.Size_1280_720;
                    break;
                case "1280 x 768":
                    ScreenRes = ScreenResolution.Size_1280_768;
                    break;
                case "1280 x 800":
                    ScreenRes = ScreenResolution.Size_1280_800;
                    break;
                case "1280 x 960":
                    ScreenRes = ScreenResolution.Size_1280_960;
                    break;
                case "1280 x 1024":
                    ScreenRes = ScreenResolution.Size_1280_1024;
                    break;
            }
            #endregion

            #region  switch (Str_GfxMode)
            switch (Str_GfxMode)
            {
                case "GfxAutoDetect":
                    GfxMode = GfxModes.GfxAutoDetect;
                    break;
                case "GfxAutoDetectFullScreen":
                    GfxMode = GfxModes.GfxAutoDetectFullScreen;
                    break;
                case "GfxAutoDetectWindowed":
                    GfxMode = GfxModes.GfxAutoDetectWindowed;
                    break;
                case "GfxDirectXAccel":
                    GfxMode = GfxModes.GfxDirectXAccel;
                    break;
                case "GfxDirectXWin":
                    GfxMode = GfxModes.GfxDirectXWin;
                    break;
                case "GfxDirectX":
                    GfxMode = GfxModes.GfxDirectX;
                    break;
                case "GfxDirectXSafe":
                    GfxMode = GfxModes.GfxDirectXSafe;
                    break;
                case "GfxDirectXSoft":
                    GfxMode = GfxModes.GfxDirectXSoft;
                    break;
                case "GfxDirectXOlv":
                    GfxMode = GfxModes.GfxDirectXOlv;
                    break;
                case "GfxGDI":
                    GfxMode = GfxModes.GfxGDI;
                    break;
            }
            #endregion

            #region switch (Str_RenderingMode)
            switch (Str_RenderingMode)
            {
                case "DoubleBuffering":
                    RenderingMode = RenderingModes.DoubleBuffering;
                    break;
                case "TripleBuffering":
                    RenderingMode = RenderingModes.TripleBuffering;
                    break;
                case "PageFlipping":
                    RenderingMode = RenderingModes.PageFlipping;
                    break;

            }
            #endregion

            #region switch (Str_BmpType)
            switch (Str_BmpType)
            {
                case "Memory":
                    BmpTyp = BmpType.Memory;
                    break;
                case "System":
                    BmpTyp = BmpType.System;
                    break;
                case "Video":
                    BmpTyp = BmpType.Video;
                    break;

            }
            #endregion
            #endregion

            //Initialize AllegNet
            AllegNetInit();

            //Install Keyboard
            InstallKeyboard();

            //Install Timer
            InstallTimer();

            InstallMouse();

            InstallSound(DigiWindows.DigiAutoDetect, MidiWindows.MidiNone, "");

            //Set the application running on background
            SetDisplaySwitchMode(SwitchModes.SwitchBackground);

            //Set the render mode
            SetRenderMode(RenderingMode, BmpTyp);

            //Set the colorDepth
            SetColorDepth(CDepth.Depth_32_Bits); //32 Bits cause only this mode can accept ARGB

            //Set the Gfx Mode
            SetGfxMode(GfxMode, ScreenRes);

            //Declare a callback delegate to use with timer
            Cproc cTimerFct = new Cproc(TimerFct);

            //Install cTimerFct to be calledback each 1000 ms (1 second)
            InstallInt(cTimerFct, 1000);

            //Initialize main loop condition
            bool Done = false;

            int PanelLR = 128;

            Mp3File Mp3Test = new Mp3File();
            Mp3Test.LoadFromFile(@"ExMp3.mp3");
            
            bool KbEnter = false;
            bool IsPlaying = false;
            int LastFrame = 0;
            //Main Loop
            while (!Done)
            {
                //Acquire the Buffer
                AcquireBitmap(BufferedScreen);
                //Clear the buffer
                ClearBitmap(BufferedScreen);


                TextOutEx(BufferedScreen, DefaultFont, "Mp3 file reader example : ExMp3.mp3" , 10, 10, MakeCol(255, 255, 255), 0);
                
                TextOutEx(BufferedScreen, DefaultFont, "Reading mp3 frame " + Mp3Test.CurrentFrame.ToString() + " / " + Mp3Test.TotalFrames.ToString(), 10, 20, MakeCol(255, 255, 255), 0);

                TextOutEx(BufferedScreen, DefaultFont, "State : " , 10, 40, MakeCol(255, 255, 255), 0);

                TextOutEx(BufferedScreen, DefaultFont, IsPlaying ? "Play >>" : "Stop ||", 70, 40, IsPlaying ? System.Drawing.Color.Green : System.Drawing.Color.Red, System.Drawing.Color.Black);

                TextOutEx(BufferedScreen, DefaultFont, "Enter Pad to play ", 10, 60, MakeCol(255, 255, 255), 0);

                TextOutEx(BufferedScreen, DefaultFont, "Del Pad to Stop ", 10, 70, MakeCol(255, 255, 255), 0);

                
                //Release the Buffer
                ReleaseBitmap(BufferedScreen);

                //Render to screen with actual render settings
                ApiRenderer.Render();

                if (IsPlaying && Mp3Test.CurrentFrame < LastFrame)
                {
                    KbEnter = false;
                    Mp3Test.Stop();
                    IsPlaying = false;
                    Mp3Test.Stop();
                }

                if (KbKey[AllKeys.EnterPad])
                {
                    if (!KbEnter)
                    {
                        KbEnter = true;
                        Mp3Test.Play(512,128);
                        IsPlaying = true;
                    }                    
                }

                if (KbKey[AllKeys.DelPad])
                {
                    if (KbEnter)
                    {
                        KbEnter = false;
                        Mp3Test.Stop();
                        IsPlaying = false;
                    }
                }

                LastFrame = Mp3Test.CurrentFrame;

                //Check for KeyPressed
                if (KbKey[AllKeys.Esc])
                    Done = true;
                Fps++;
            }
            //Exit AllegNet
            UninstallAllegNet();
        }

        /// <summary>
        /// Timer function
        /// </summary>
        void TimerFct()
        {
            FpsToShow = Fps;
            Fps = 0;
        }
    }
}
